package com.dwcode.macro.controller;

import com.dwcode.macro.dto.Pagination;
import com.dwcode.macro.dto.ResultInfo;
import com.dwcode.macro.entity.TUser;
import com.dwcode.macro.service.TUserService;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * (TUser)表控制层
 *
 * @author makejava
 * @since 2020-09-17 22:05:01
 */
@Controller
@RequestMapping("/user")
@Slf4j
public class TUserController {
    /**
     * 服务对象
     */
    @Resource
    private TUserService tUserService;

    /**
     * 通过主键查询单条数据
     *
     * @param id 主键
     * @return 单条数据
     */
    @GetMapping("/getUser/{id}")
    @ApiOperation("根据id主键查用户信息")
    public @ResponseBody TUser selectOne(@PathVariable("id")Integer id) {
        return this.tUserService.queryById(id);
    }

    /**
     * 修改用户信息
     * @param user
     * @return
     */
    @PutMapping("/setUser")
    @ApiOperation("修改用户信息")
    public @ResponseBody
    ResultInfo<Pagination<TUser>> updateUser(TUser user, Pagination<TUser> pagination ){
        ResultInfo<Pagination<TUser>> resultInfo = new ResultInfo<>();
        TUser update = tUserService.update(user);
        if(update != null){
            resultInfo.setCode(1);
            resultInfo.setMsg("修改用户信息成功");
        }
        pagination = tUserService.getUsers(pagination);
//        pagination.setRows();
        resultInfo.setResult(pagination);
        return resultInfo;
    }
    @DeleteMapping("/deleteUser/{id}")
    @ApiOperation("根据ID删除用户信息")
    public @ResponseBody ResultInfo<Pagination<TUser>> deleteUser(@PathVariable("id")Integer id, Pagination<TUser> pagination){
        ResultInfo<Pagination<TUser>> resultInfo = new ResultInfo<>();
        boolean b = tUserService.deleteById(id);
        if (b){
            resultInfo.setCode(1);
            resultInfo.setMsg("删除成功");
        }
        pagination = tUserService.getUsers(pagination);
        resultInfo.setResult(pagination);
        return resultInfo;
    }

    @ResponseBody
    @GetMapping("/getUsers")
    @ApiOperation("获取分页用户列表")
    public Pagination<TUser> getUsers(Pagination pagination){
        return tUserService.getUsers(pagination);
    }

    @GetMapping("/search")
    @ApiOperation("搜索用户信息")
    public @ResponseBody Pagination<TUser> search(TUser user, Pagination<TUser> pagination, Model model){
        pagination = tUserService.findUserSearch(user, pagination);
        return pagination;
    }

    /**
     * 批量删除
     */
    @GetMapping("/deleteBatch")
    public @ResponseBody ResultInfo<Pagination<TUser>> deleteBatch(String ids,Pagination pagination){
        log.info(ids);
        String[] iss = ids.split(",");
        int[] array = Arrays.asList(iss).stream().mapToInt(Integer::parseInt).toArray();
        boolean i = tUserService.deleteBatch(array);
        ResultInfo<Pagination<TUser>> resultInfo = new ResultInfo<>();
        if (i){
            resultInfo.setCode(1);
            resultInfo.setMsg("批量删除成功");
        }else{
            resultInfo.setCode(0);
            resultInfo.setMsg("批量删除失败");
            return resultInfo;
        }
        pagination = tUserService.getUsers(pagination);
        resultInfo.setResult(pagination);
        return resultInfo;
    }

}